package org.alis.ap.rpc;

import lombok.*;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * ServiceDescriptor desc: 表示服务
 *
 * @author luochuan
 * @date 2020/10/16 14:57
 */
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class ServiceDescriptor {
    private String clazz;
    private String method;
    private String returnType;
    private String[] parameterTypes;
    public static  ServiceDescriptor from(Class<?> clazz, Method method){
        ServiceDescriptor serviceDescriptor = new ServiceDescriptor();
        serviceDescriptor.setClazz(clazz.getName());
        serviceDescriptor.setMethod(method.getName());
        serviceDescriptor.setReturnType(method.getReturnType().getName());
        serviceDescriptor.parameterTypes= Arrays.stream(method.getParameterTypes()).map(Class::getName).toArray(String[]::new);
        return serviceDescriptor;
    }

    @Override
    public boolean equals(Object s){
        if(Boolean.FALSE.equals(s instanceof ServiceDescriptor)){
            return false;
        }
        if(Boolean.TRUE.equals(super.equals(s))){
            return true;
        }
        return StringUtils.equals(this.toString(),s.toString());
    }


    @Override
    public int hashCode(){
        return toString().hashCode();
    }

    @Override
    public String toString() {
        return "ServiceDescriptor{" +
                "clazz='" + clazz + '\'' +
                ", method='" + method + '\'' +
                ", returnType='" + returnType + '\'' +
                ", parameterTypes=" + Arrays.toString(parameterTypes) +
                '}';
    }
}
